package flower.com.a600_xm.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import de.greenrobot.event.EventBus;
import flower.com.a600_xm.R;
import flower.com.a600_xm.base.Base_Z_Adapter;
import flower.com.a600_xm.base.MyApplication;
import flower.com.a600_xm.bean.MyEvent;
import flower.com.a600_xm.db.Buy_ShoppingInfoDao;
import flower.com.a600_xm.db.dbbean.Buy_Shopping;
import flower.com.a600_xm.db.dbbean.Life_Shopping;
import flower.com.a600_xm.ui.NumberButton;


/**
 * Created by 他的猫 on 2016/12/3.
 */

public class Li_Adapter extends Base_Z_Adapter {
    Li_Adapter.Holder holder;
    Buy_ShoppingInfoDao buy_shoppingInfoDao;

    public Li_Adapter() {
        buy_shoppingInfoDao = new Buy_ShoppingInfoDao(MyApplication.getInstance());
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        // 查找控件
        if (null == convertView) {
            convertView = LayoutInflater.from(MyApplication.getInstance())
                    .inflate(R.layout.tab_item_1_grid, null);
            holder = new Li_Adapter.Holder();
            holder.name = (TextView) convertView
                    .findViewById(R.id.name);
            holder.price = (TextView) convertView
                    .findViewById(R.id.price2);
            holder.numberButton = (NumberButton) convertView
                    .findViewById(R.id.number_button);

            // 缓存Holder
            convertView.setTag(holder);
        } else {
            holder = (Li_Adapter.Holder) convertView.getTag();
        }
        Buy_Shopping item = (Buy_Shopping) getItem(position);
        holder.name.setText(item.getName());
        holder.price.setText(item.getPrice());
        holder.numberButton.setNumber(item.getNumber());
        holder.numberButton.setOnWarnListener(new NumberButton.OnWarnListener() {
            @Override
            public void onWarningForInventory(String inventory) {
                Log.e(TAG, "zzzzzzzz:" + inventory);
                Buy_Shopping item = (Buy_Shopping) getItem(position);
                buy_shoppingInfoDao.updata(item.getName(), inventory);
                MyEvent eventBus = new MyEvent();
                eventBus.setType("dong");
                EventBus.getDefault().post(eventBus);
            }


        });

        return convertView;
    }

    public class Holder {
        TextView name, price, spec;
        ImageView image, im_add;
        NumberButton numberButton;

    }
}
